// pages/activity/activity.js
var app=getApp();
var config=require("../../api/config");
var util=require("../../utils/util");

Page({
  /*页面的初始数据*/
  data: {
    id: '',
    info: '',
    user_list: [],
    pixelRate: '',
    statusBarHeight: '',
    capsuleHeight: 44,
    scrollLeft: 0,
    isStart: false,
    ret: '',
    idx: 0,
    timer: '',
    num: 0,
    hide: false,
    show_mask: true,
    show_pop_mask: true,
    show_rule: true,
    prizes:[
      {status:1,prize:'5',money:"5"},
      {status:2,prize:'10',money:"10"},
      {status:1,prize:'20',money:"20"},
      {status:1,prize:'30',money:"30"},
      {status:1,prize:'80',money:"80"},
      {status:2,prize:'100',money:"100"},
      {status:1,prize:'200',money:"200"},
      {status:2,prize:'300',money:"300"},
    ],
    token: '',
    version: '',
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var token=wx.getStorageSync('token');
    var sysinfo = wx.getSystemInfoSync(),pixelRate=sysinfo.windowWidth/750,statusBarHeight=sysinfo.statusBarHeight;
    that.setData({id:options.id,user_list:config.activityData,pixelRate:pixelRate,statusBarHeight:statusBarHeight,token:token})
    that.scroll_list();
    that.get_activity_type(options.id,token)
  },
  // 返回
  nav_back:function(){
    wx.navigateBack({
      delta: 1,
      success:function(){},
      fail:function(){
        wx.switchTab({
          url: '../home/home',
        })
      }
    });
  },
  //滚动
  scroll_list:function(){
    var that=this,scrollLeft=that.data.scrollLeft,user_list=that.data.user_list,num=0,number=0;
    that.data.timer=setInterval(function(){
      scrollLeft++
      num++;
      that.setData({scrollLeft:scrollLeft})
      if(num%60==0){
        number++
        user_list.push(user_list[number])
        that.setData({user_list:user_list})
      }
    },50)
  },
  // 抽奖
  start:function(){
    var that=this,isStart=that.data.isStart,num=that.data.num,token=that.data.token;
    if(token){
      if(isStart){return false;}
      if(num>0){
        num=num-1
        that.setData({isStart:true,num:num})
        //
        that.user_get_money();
        // that.user_luck(3)
      }else{
        util.page_show_toast('抽奖机会已用完',2000)
      }
    }else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  // 跑圈中奖
  user_luck:function(ret){
    var that=this,idx=that.data.idx;
    var range=5,count=idx,len=8;// range跑圈可变/len=8不可变
    var timer1=setInterval(function(){
      count++
      if(count==range*len){
        clearInterval(timer1)
        var timer2=setInterval(function(){
          count++
          if(count==(range+1)*len+ret){
            clearInterval(timer2)
            setTimeout(()=>{that.setData({show_mask:false})},500)
          }
          that.setData({idx:count%8==0?8:count%8})
        }, 150)
      }
      that.setData({idx:count%8==0?8:count%8})
    },80)
  },
  rule_page:function(){
    this.setData({show_rule:!this.data.show_rule})
  },
  open_preview:function(){
    wx.previewImage({
      urls: ["https://ossdev0.jiabala.com/NRTh8dZwK6nRH"],
    })
  },
  nav_home:function(){
    wx.switchTab({
      url: '../home/home',
    })
  },
  // 弹窗
  close_mask:function(e){
    var that=this;
    var type=e.currentTarget.dataset.type;
    if(type==1){
      util.page_show_toast('领取成功',2000)
    }
    that.setData({show_mask:!that.data.show_mask,isStart:false,idx:''})
  },
  return:function(){},
  // 清除滚动/打开滚动
  onHide:function(){
    var that=this;
    clearInterval(that.data.timer)
    that.setData({hide:true})
  },
  onUnload:function(){
    var that=this;
    clearInterval(that.data.timer)
  },
  onShow:function(){
    var that=this,hide=that.data.hide,id=that.data.id;
    if(hide){
      that.scroll_list()
      that.setData({hide:false})
    }
    if(app.globalData.login){
      app.globalData.login=false;
      app.globalData.change_dom=false;
      app.globalData.change_table=false;
      var token=wx.getStorageSync('token');
      //
      that.setData({token:token})
      that.get_user_count(id,token)
    }
  },
  // 分享
  onShareAppMessage:function(){
    var that=this,id=that.data.id,name=that.data.activity_name;
    return{
      title: name,
      path: '/pages/activity/activity?id='+id,
      imageUrl: 'https://ossdev0.jiabala.com/pXwxbDTQSWKhi'
    }
  },
  onShareTimeline:function(){
    var that=this,name=that.data.activity_name;
    return{
      title: name,
      imageUrl: 'https://ossdev0.jiabala.com/yS27QADatw6n5'
    }
  },
  // 查看活动是否过期
  get_activity_type:function(id,token){
    var that=this,id=that.data.id;
    var params={
      "activityId": id
    }
    config.get_activity_type(params).then(res=>{
      if(res.data.code==200){
        if(res.data.data){
          var info=res.data.data;
          if(token){
            that.get_user_count(id,token)
          }
          that.setData({version:info.version,activity_name:info.name})
        }else{
          that.setData({show_pop_mask:false})
        }
      }
    })
  },
  // 获取登陆用户的抽奖次数
  get_user_count:function(id,token){
    var that=this,id=that.data.id;
    var params={
      "activityId": id
    }
    config.get_user_count(params,token).then(res=>{
      if(res.data.code==200){
        that.setData({num:res.data.data.surplusNum})
      }
    })
  },
  // 用户抽奖
  user_get_money:function(){
    var that=this,id=that.data.id,prizes=that.data.prizes,version=that.data.version,token=that.data.token,ret=1;
    var params={
      "couponId": id,
      "version": version?version:1
    }
    config.user_get_money(params,token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        if(info.status==2){
          for(var i=0;i<prizes.length;i++){
            if(info.money==prizes[i].money){
              ret=i+1
            }
          }
          that.user_luck(ret)
          that.setData({ret:ret})
        }else if(info.status==1){
          util.page_show_toast("活动金额已抽完",2000)
        }
      }
    })
  },
})